#include<iostream>
using namespace std;
#include<vector>
class Solution {
public:
    int countKConstraintSubstrings(string s, int k) {
        int ans = 0;
        int arr[2] = { 0 };
        for (int left = 0, right = 0; right < s.size(); ++right) {
            ++arr[s[right] - '0'];
            if (arr[0] <= k || arr[1] <= k)
                ans += right - left + 1;
            else {
                while (arr[0] > k && arr[1] > k)
                    --arr[s[left++] - '0'];
                ans += right - left + 1;
            }
        }
        return ans;
    }
};